package com.xuecheng.learning.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.learning.domain.feignclient.CourseServiceClient;
import com.xuecheng.learning.domain.mapper.XcChooseCourseMapper;
import com.xuecheng.learning.domain.model.entity.XcChooseCourse;
import com.xuecheng.learning.domain.model.enums.XcCourseChargeEnum;
import com.xuecheng.learning.domain.model.enums.XcCourseOrderEnum;
import com.xuecheng.learning.domain.model.enums.XcCourseStatusEnum;
import com.xuecheng.learning.domain.model.vo.resp.XcChooseCourseResp;
import com.xuecheng.learning.domain.service.XcChooseCourseService;
import com.xuecheng.learning.domain.service.XcCourseTablesService;
import com.xuecheng.learning.domain.service.XcLearnRecordService;
import com.xuecheng.learning.infrastructure.vo.CoursePublish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class XcChooseCourseServiceImpl extends ServiceImpl<XcChooseCourseMapper, XcChooseCourse> implements XcChooseCourseService {

    @Autowired
    private XcChooseCourseMapper xcChooseCourseMapper;

    @Autowired
    private CourseServiceClient courseServiceClient;

    @Autowired
    private XcCourseTablesService xcCourseTablesService;

    @Autowired
    private XcLearnRecordService xcLearnRecordService;

    @Override
    public XcChooseCourseResp chooseCourse(Long userId, Long courseId) {
        CoursePublish coursepublish = courseServiceClient.getCoursepublish(courseId);
        if (Objects.isNull(coursepublish)){
            throw new XueChengPlusException("课程不存在");
        }

        XcChooseCourse xcChooseCourse;
        if (XcCourseChargeEnum.FREE.getCode().equals(coursepublish.getCharge())){
            // 收费
             xcChooseCourse = addChargeCourse(userId, coursepublish);
        } else{
            // 免费
            xcChooseCourse = addFreeCourse(userId, coursepublish);
            // 添加到我都课表
            xcCourseTablesService.addCourseTable(xcChooseCourse);
        }
        XcChooseCourseResp xcChooseCourseResp = BeanUtil.copyProperties(xcChooseCourse, XcChooseCourseResp.class);
        xcChooseCourseResp.setLearnStatus(xcLearnRecordService.getLearnStatus(userId, courseId));

        return xcChooseCourseResp;
    }

    private XcChooseCourse addChargeCourse(Long userId, CoursePublish coursepublish) {
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(new LambdaQueryWrapper<XcChooseCourse>()
                .eq(XcChooseCourse::getUserId, userId)
                .eq(XcChooseCourse::getCourseId, coursepublish.getId())
                .eq(XcChooseCourse::getOrderType, XcCourseOrderEnum.NO_FREE.getCode())
                .eq(XcChooseCourse::getStatus, XcCourseStatusEnum.SUCCESS.getCode()));
        if (CollUtil.isNotEmpty(xcChooseCourses)){
            // 若已经选课成功就返回
            return xcChooseCourses.get(0);
        }

        XcChooseCourse xcChooseCourse = XcChooseCourse.buildCourse(userId, coursepublish, XcCourseOrderEnum.NO_FREE.getCode(),
                XcCourseStatusEnum.SUCCESS.getCode());
        xcChooseCourseMapper.insert(xcChooseCourse);

        return xcChooseCourse;
    }

    /**
     * 添加免费课程
     * @param userId
     * @param coursepublish
     */
    private XcChooseCourse addFreeCourse(Long userId, CoursePublish coursepublish) {
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(new LambdaQueryWrapper<XcChooseCourse>()
                .eq(XcChooseCourse::getUserId, userId)
                .eq(XcChooseCourse::getCourseId, coursepublish.getId())
                .eq(XcChooseCourse::getOrderType, XcCourseOrderEnum.FREE.getCode())
                .eq(XcChooseCourse::getStatus, XcCourseStatusEnum.SUCCESS.getCode()));
        if (Objects.nonNull(xcChooseCourses)){
            // 若已经选课成功就返回
            return xcChooseCourses.get(0);
        }

        XcChooseCourse xcChooseCourse = XcChooseCourse.buildCourse(userId, coursepublish, XcCourseOrderEnum.FREE.getCode(),
                XcCourseStatusEnum.SUCCESS.getCode());
        xcChooseCourseMapper.insert(xcChooseCourse);

        return xcChooseCourse;
    }
}
